from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QPushButton, QLabel, QHBoxLayout, QWidget, QListWidget

from LanguageTool import getCNfromDict
from mylog import addLog


class toPCMBINWidget(QWidget):
    file_path = ""
    status = 0
    fmt = ""

    def __init__(self, file_path, fmt, list_widget, parent=None):
        super().__init__(parent)

        self.file_path = file_path
        self.list_widget = list_widget
        self.fmt = fmt

        layout = QHBoxLayout()
        layout.setContentsMargins(5, 5, 5, 5)

        # 状态信息
        self.status_label = QLabel('')
        self.status_label.setAlignment(Qt.AlignLeft | Qt.AlignVCenter)
        self.setStatus(self.status)
        layout.addWidget(self.status_label)

        # 格式信息
        self.fmt_label = QLabel(self.fmt)
        self.fmt_label.setStyleSheet('color:hotpink')
        self.fmt_label.setAlignment(Qt.AlignLeft | Qt.AlignVCenter)
        layout.addWidget(self.fmt_label)

        # 左侧显示文件路径
        path_label = QLabel(file_path)
        path_label.setAlignment(Qt.AlignRight | Qt.AlignVCenter)
        layout.addWidget(path_label)

        # 添加伸缩因子，将标签靠左
        layout.addStretch(1)

        # # 右侧播放按钮
        # toMP3_play_button = QPushButton("播放", self)
        # toMP3_play_button.clicked.connect(self.toMP3_play_button_clicked)  # 按钮事件绑定
        # toMP3_play_button.setFixedWidth(100)
        # layout.addWidget(toMP3_play_button)

        # 右侧播放按钮
        self.del_button = QPushButton(getCNfromDict('TabBar01-tb1-delbtn'), self)
        self.del_button.clicked.connect(self.del_button_clicked)  # 按钮事件绑定
        self.del_button.setFixedWidth(100)
        layout.addWidget(self.del_button)

        # 设置布局
        self.setLayout(layout)

    # def toMP3_play_button_clicked(self):
    #     pass

    def del_button_clicked(self):
        """
        删除当前项
        """
        # 通过小部件找到对应的 QListWidgetItem
        addLog('toPCMBINWidget', 'info', f"del_button_clicked")
        for index in range(self.list_widget.count()):
            item = self.list_widget.item(index)
            if self.list_widget.itemWidget(item) is self:
                # 删除该项
                addLog('toPCMBINWidget', 'info', f"del {self.file_path} finish!")
                self.list_widget.takeItem(index)
                break

    def getPath(self):
        return self.file_path

    def setStatus(self, status):
        self.status = status
        if status == 1:
            self.status_label.setText('已完成')
            self.status_label.setStyleSheet('color: blue')
        elif status == 0:
            self.status_label.setText('待执行')
            self.status_label.setStyleSheet('color: gray')
        elif status == 2:
            self.status_label.setText('失败')
            self.status_label.setStyleSheet('color: red')
        addLog('toPCMBINWidget', 'info', f"set {self.file_path} Status {self.status}")

    def getStatus(self):
        return self.status

    def getFmt(self):
        return self.fmt
